/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClickConfigPanel.java
 *
 * Created on Sep 8, 2009, 7:58:16 PM
 */
package org.netbeans.modules.web.click;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import org.netbeans.api.project.libraries.Library;
import org.netbeans.api.project.libraries.LibraryManager;
import org.openide.util.Exceptions;

/**
 *
 * @author hantsy
 */
public class ClickConfigurationPanel extends javax.swing.JPanel {

    /** Creates new form ClickConfigPanel */
    boolean active = true;
    private final String DEFAULT_PAGES_PACKAGE = ClickConstants.DEFAULT_PACKAGE_NAME;

    public ClickConfigurationPanel(boolean defaultValue) {
        this.active = defaultValue;
        if (active) {
            initComponents();
            initLibraries();
        }
    }
    List<Library> clickLibraries = null;

    private void initLibraries() {
        Vector<String> items = new Vector<String>();
        clickLibraries = new ArrayList<Library>();
        Library defaultLibrary = null;
        List<URL> content;
        for (Library library : LibraryManager.getDefault().getLibraries()) {
            content = library.getContent("classpath"); //NOI18N
            try {
                if (ClickConfigUtilities.containsClass(content, ClickConstants.CLICK_SERVELT_CLASS)) {
                    items.add(library.getDisplayName());
                    clickLibraries.add(library);
                    if (ClickConstants.LIBRARY_CLICK.equals(library.getName())) {
                        defaultLibrary = library;
                    }
                }
            } catch (IOException exception) {
                Exceptions.printStackTrace(exception);
            }
        }

        cbxLibraries.setModel(new DefaultComboBoxModel(items));
        if (items.size() == 0) {
            rbRegLib.setEnabled(false);
            cbxLibraries.setEnabled(false);
            rbNoneLib.setSelected(true);
            setSelectedLibrary(null);
        } else {
            rbRegLib.setEnabled(true);
            rbRegLib.setSelected(true);
            cbxLibraries.setEnabled(true);
            cbxLibraries.setSelectedItem(defaultLibrary.getDisplayName());
            setSelectedLibrary(defaultLibrary);
        }
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rbButtonGroup = new javax.swing.ButtonGroup();
        configPanel = new javax.swing.JTabbedPane();
        mainPanel = new javax.swing.JPanel();
        lbPagesPackage = new javax.swing.JLabel();
        txtPagesPackage = new javax.swing.JTextField();
        cbxSpringSupport = new javax.swing.JCheckBox();
        cbxCompressionFilter = new javax.swing.JCheckBox();
        cbxPerfermanceFilter = new javax.swing.JCheckBox();
        lbMode = new javax.swing.JLabel();
        cmbMode = new javax.swing.JComboBox();
        lbPanel = new javax.swing.JPanel();
        rbNoneLib = new javax.swing.JRadioButton();
        rbRegLib = new javax.swing.JRadioButton();
        cbxLibraries = new javax.swing.JComboBox();

        setLayout(new java.awt.BorderLayout());

        lbPagesPackage.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.lbPagesPackage.text")); // NOI18N

        txtPagesPackage.setText(DEFAULT_PAGES_PACKAGE);

        cbxSpringSupport.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.cbxSpringSupport.text")); // NOI18N

        cbxCompressionFilter.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.cbxCompressionFilter.text")); // NOI18N

        cbxPerfermanceFilter.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.cbxPerfermanceFilter.text")); // NOI18N

        lbMode.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.lbMode.text")); // NOI18N

        cmbMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "production", "profile", "development", "debug", "trace" }));
        cmbMode.setSelectedItem("development");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbMode)
                    .addComponent(lbPagesPackage))
                .addGap(2, 2, 2)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(txtPagesPackage, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                        .addGap(22, 22, 22))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(cmbMode, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxCompressionFilter)
                .addContainerGap(197, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxPerfermanceFilter)
                .addContainerGap(203, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxSpringSupport)
                .addContainerGap(239, Short.MAX_VALUE))
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lbMode, lbPagesPackage});

        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPagesPackage)
                    .addComponent(txtPagesPackage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbMode)
                    .addComponent(cmbMode, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(cbxSpringSupport)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbxCompressionFilter)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbxPerfermanceFilter)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cbxCompressionFilter, cbxPerfermanceFilter, cbxSpringSupport});

        configPanel.addTab(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.mainPanel.TabConstraints.tabTitle"), mainPanel); // NOI18N

        rbButtonGroup.add(rbNoneLib);
        rbNoneLib.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.rbNoneLib.text")); // NOI18N
        rbNoneLib.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbNoneLibActionPerformed(evt);
            }
        });

        rbButtonGroup.add(rbRegLib);
        rbRegLib.setText(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.rbRegLib.text")); // NOI18N
        rbRegLib.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbRegLibActionPerformed(evt);
            }
        });

        cbxLibraries.setMinimumSize(new java.awt.Dimension(120, 21));
        cbxLibraries.setPreferredSize(new java.awt.Dimension(120, 21));
        cbxLibraries.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxLibrariesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lbPanelLayout = new javax.swing.GroupLayout(lbPanel);
        lbPanel.setLayout(lbPanelLayout);
        lbPanelLayout.setHorizontalGroup(
            lbPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lbPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lbPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lbPanelLayout.createSequentialGroup()
                        .addComponent(rbRegLib)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbxLibraries, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(rbNoneLib))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        lbPanelLayout.setVerticalGroup(
            lbPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lbPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(lbPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbRegLib)
                    .addComponent(cbxLibraries, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbNoneLib)
                .addContainerGap(90, Short.MAX_VALUE))
        );

        configPanel.addTab(org.openide.util.NbBundle.getMessage(ClickConfigurationPanel.class, "ClickConfigurationPanel.lbPanel.TabConstraints.tabTitle"), lbPanel); // NOI18N

        add(configPanel, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void rbRegLibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbRegLibActionPerformed
        setSelectedLibrary(clickLibraries.get(cbxLibraries.getSelectedIndex()));
    }//GEN-LAST:event_rbRegLibActionPerformed

    private void rbNoneLibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbNoneLibActionPerformed
        setSelectedLibrary(null);
    }//GEN-LAST:event_rbNoneLibActionPerformed

    private void cbxLibrariesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxLibrariesActionPerformed
        setSelectedLibrary(clickLibraries.get(cbxLibraries.getSelectedIndex()));
    }//GEN-LAST:event_cbxLibrariesActionPerformed
    private Library selectedLibrary;

    public Library getSelectedLibrary() {
        return selectedLibrary;
    }

    public void setSelectedLibrary(Library selectedLibrary) {
        this.selectedLibrary = selectedLibrary;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cbxCompressionFilter;
    private javax.swing.JComboBox cbxLibraries;
    private javax.swing.JCheckBox cbxPerfermanceFilter;
    private javax.swing.JCheckBox cbxSpringSupport;
    private javax.swing.JComboBox cmbMode;
    private javax.swing.JTabbedPane configPanel;
    private javax.swing.JLabel lbMode;
    private javax.swing.JLabel lbPagesPackage;
    private javax.swing.JPanel lbPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.ButtonGroup rbButtonGroup;
    private javax.swing.JRadioButton rbNoneLib;
    private javax.swing.JRadioButton rbRegLib;
    private javax.swing.JTextField txtPagesPackage;
    // End of variables declaration//GEN-END:variables

    public String getPagesPackage() {
        return txtPagesPackage.getText();
    }


    public boolean compressionFilterEnabled() {
        return cbxCompressionFilter.isSelected();
    }

    public boolean perfermanceFilterEnabled() {
        return cbxPerfermanceFilter.isSelected();
    }

    public boolean supportSpring() {
        return cbxSpringSupport.isSelected();
    }

    public String getMode() {
        return cmbMode.getSelectedItem().toString();
    }
}
